﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace XISoft.AppCreator.CommonUI.UML
{
    public partial class ItemHeader : UserControl
    {
        private const int _height = 16;

        private bool _isSelected = false;

        public ItemHeader()
        {
            InitializeComponent();            
            _Drawstring = "属性";
        }
        public ItemHeader(ClassSon _classson, ItemPanel itempannel)
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            switch (_classson)
            {
                case ClassSon.Event:
                    _Drawstring = "事件";
                    break;
                case ClassSon.Method:
                    _Drawstring = "方法";
                    break;
                case ClassSon.Attribute:
                    _Drawstring = "属性";
                    break;
                case ClassSon.Filed:
                    _Drawstring = "字段";
                    break;
            }
            _itempannel = itempannel;
            this.BackColor = Color.Transparent;
            this.Size = new System.Drawing.Size(_itempannel.Width, _height);
            this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right))); this.Refresh();
        }

        private string _Drawstring = string.Empty;     
        private ItemPanel _itempannel = null;


        private void ItemHeader_Paint(object sender, PaintEventArgs e)
        {
            if (!_itempannel.isExpand)
            { 
                e.Graphics.DrawLine(new Pen(Color.Thistle), new Point(12, 7), new Point(16, 7));
                e.Graphics.DrawLine(new Pen(Color.Thistle), new Point(14, 5), new Point(14, 9));          
                
            }
            else
            {
                e.Graphics.DrawLine(new Pen(Color.Thistle), new Point(12, 7), new Point(16, 7));
            }
            e.Graphics.DrawString(_Drawstring, new Font("宋体", 9), new SolidBrush(Color.Black), new PointF(25, 2));
            e.Graphics.DrawRectangle(new Pen(Color.Thistle), new Rectangle(10, 3, 8, 8));
            if (_isSelected)
            {
                Pen selPen = new Pen(Color.Gray);
                selPen.DashStyle = DashStyle.Dot;
              //  e.Graphics.DrawRectangle(selPen, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
                e.Graphics.DrawString(_Drawstring, new Font(new FontFamily("宋体"), 9), new SolidBrush(Color.White), new PointF(25, 2));
            }
        }

        private void ItemHeader_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.X >= 10 && e.X <= 18 && e.Y >= 3 && e.Y <= 21)
                {
                    _itempannel.change();
                    
                }
            }
            this.Focus();
            this.BackColor = Color.DarkBlue;
            this._isSelected = true;
        }
        private void ItemHeader_Load(object sender, EventArgs e)
        {
            this.Height = _height;
        }

        private void ItemHeader_Leave(object sender, EventArgs e)
        {
            this.BackColor = Color.Transparent;
            this._isSelected = false;
        }

        private void ItemHeader_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
               int index= this._itempannel.Controls.IndexOf(this);
               if (index < this._itempannel.Controls.Count - 1)
               {
                   this._itempannel.Controls[index + 1].Focus();
               }
               else
               {
                   _itempannel.Controls[0].Focus();
               }
            }
            if (e.KeyCode == Keys.Up)
            {
                int index = this._itempannel.Controls.IndexOf(this);
                if (index >0)
                {
                    this._itempannel.Controls[index - 1].Focus();
                }
                else
                {
                    _itempannel.Controls[_itempannel.Controls.Count-1].Focus();
                }
            }
        }

        private void ItemHeader_Enter(object sender, EventArgs e)
        {
           // this.BackColor = Color.DarkBlue;
        }
    }
}
